package com.iye.puretennis.activity.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.*;
import com.iye.puretennis.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Chuanbao on 2014/11/5.
 */
public class DropdownTestActivity extends Activity {

    /** Called when the activity is first created. */
    private List<User> list = new ArrayList<User>();
    private TextView myTextView;
    private Spinner mySpinner;
    private ArrayAdapter<User> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout);

        User user1 = this.new User().newUser("001","张三");
        User user2 = this.new User().newUser("002","李四");
        User user3 = this.new User().newUser("003","王五");
        User user4 = this.new User().newUser("004","王麻子");
        User user5 = this.new User().newUser("005","李胖子");
        //第一步：添加一个下拉列表项的list，这里添加的项就是下拉列表的菜单项
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        myTextView = (TextView)findViewById(R.id.to_user);
        mySpinner = (Spinner)findViewById(R.id.spinner_userlist);
        //第二步：为下拉列表定义一个适配器，这里就用到里前面定义的list。
        adapter = new ArrayAdapter<User>(this,android.R.layout.simple_spinner_item, list);
        //第三步：为适配器设置下拉列表下拉时的菜单样式。
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //第四步：将适配器添加到下拉列表上
        mySpinner.setAdapter(adapter);
        //第五步：为下拉列表设置各种事件的响应，这个事响应菜单被选中
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            @SuppressWarnings("unchecked")
            public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                /* 将所选mySpinner 的值带入myTextView 中*/
//                myTextView.setText("您选择的是："+ adapter.getItem(arg2));
                /* 将mySpinner 显示*/
                arg0.setVisibility(View.VISIBLE);
            }
            @SuppressWarnings("unchecked")
            public void onNothingSelected(AdapterView arg0) {
                // TODO Auto-generated method stub
                myTextView.setText("NONE");
                arg0.setVisibility(View.VISIBLE);
            }
        });
        /*下拉菜单弹出的内容选项触屏事件处理*/
        mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                /* 将mySpinner 隐藏，不隐藏也可以，看自己爱好*/
//                v.setVisibility(View.INVISIBLE);
                return false;
            }
        });
        /*下拉菜单弹出的内容选项焦点改变事件处理*/
        mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                v.setVisibility(View.VISIBLE);
            }
        });
    }
    private class User{
        private String guid;
        private String name;

        public String toString(){
            return name+guid;
        }

        public User newUser(String guid,String name){
            User user  = new User();
            user.setGuid(guid);
            user.setName(name);
            return user;
        }
        public String getGuid() {
            return guid;
        }

        public void setGuid(String guid) {
            this.guid = guid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}